#include "uart.h"

INT8U  uart_buffer[UART_BUFF_MAX];
const char *atSetBaud[] = { 
                            "300 ", 
                            "600 ", 
                            "1200 ", 
                            "2400 ", 
                            "4800 "
                           };
const char  uartBaud[]  = { 
                            UART_BAUD_RATE_300, 
                            UART_BAUD_RATE_600, 
                            UART_BAUD_RATE_1200, 
                            UART_BAUD_RATE_2400, 
                            UART_BAUD_RATE_4800
                           };
UART_T uart_t;

extern SYS_TIME_T  sys_time_t;
extern DIGITAL_T   digital_t;
extern SYSTEM_UI_T system_ui_t;
/*****************************************************************************
 �� �� ��: uart_init
 ��������  : ��ʼ������
 �������  : baud ������     
                      data_bit ����λ��
           parity У������
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ    :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void uart_init(INT8U _baud, INT8U _parity )
{
    SET_TIMER1_AUTO_RELOAD();
    INTERRUPT_UART_ENABLE();

    UART_RATE_DOUBLE();

    TH1 = _baud;
    TL1 = _baud;

    uart_t.parity    = _parity;
    uart_t.baud      = _baud;
    if(PARITY_NONE == uart_t.parity)
    {
        SET_UART_8_BIT();
    }
    else
    {
        SET_UART_9_BIT();
    }

    ENABLE_UART();
    TIMER1_START();

    UART_RX_EN();

    return;
}

/*****************************************************************************
 �� �� ��: uart_tx_data
 ��������  : �������ݷ���
 �������  : _data ������ָ��  
           _lenth ���ݳ���
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ    :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void uart_tx_data(INT8U *_data, INT8U _lenth)
{
    if( _lenth == 0 ) 
    {
        return;
    }

    ACC = *_data;

    switch(uart_t.parity)  
    {
        case PARITY_EVEN:
        {
            if(GET_Bit(PSW, 0))
            {
                SET_Bit(SCON, 3);
            }
            else
            {
                CLR_Bit(SCON, 3);
            }
        }
        break;
        case PARITY_ODD:
        {
            if(GET_Bit(PSW, 0))
            {
                CLR_Bit(SCON, 3);
            }
            else
            {
                SET_Bit(SCON, 3);
            }
        }
        break;
        default:
            break;
    }

    uart_t.tx_pointer  = _data;
    uart_t.tx_buff_len = _lenth;
    uart_t.state       = UART_TX_MODE;

    INTERRUPT_UART_DISABLE();

    CLR_Bit(UART_CTL_ODR, UART_TX_FLAG); 
    SBUF  = *uart_t.tx_pointer;
    uart_t.tx_pointer  ++;
    uart_t.tx_buff_len --;

    INTERRUPT_UART_ENABLE();
    return;
}

/*****************************************************************************
 �� �� ��: uart_interrupt()
 ��������  : �����жϴ�������
 �������  : ��
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ    :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void uart_interrupt(void) interrupt 4
{
    if(GET_Bit(UART_CTL_ODR, UART_TX_FLAG))
    {
        CLR_Bit(UART_CTL_ODR, UART_TX_FLAG);
        if(0 < uart_t.tx_buff_len)
            {        
                ACC = *uart_t.tx_pointer;

                switch(uart_t.parity)  
                {
                    case PARITY_EVEN:
                    {
                        if(GET_Bit(PSW, 0))
                        {
                            SET_Bit(SCON, 3);
                        }
                        else
                        {
                            CLR_Bit(SCON, 3);
                        }
                    }

                    break;
                    case PARITY_ODD:
                    {
                        if(GET_Bit(PSW, 0))
                        {
                            CLR_Bit(SCON, 3);
                        }
                        else
                        {
                            SET_Bit(SCON, 3);
                        }
                    }

                    break;
                    default:
                        break;
                }

                SBUF  = *uart_t.tx_pointer;
                uart_t.tx_pointer  ++;
                uart_t.tx_buff_len --; 
            }
            else
            {
                uart_t.state = UART_RX_MODE;
            }
    }
    else if(GET_Bit(UART_CTL_ODR, UART_RX_FLAG))
    {
        CLR_Bit(UART_CTL_ODR, UART_RX_FLAG);
        if(UART_RX_MODE == uart_t.state)
        {
            uart_t.delay_ms = 50;            

            ACC = SBUF;
            if(PARITY_CHECK())
            {
                uart_buffer[uart_t.rx_ep] = SBUF;       
                uart_t.rx_ep ++;                          
                uart_t.rx_buff_len ++;
            }
        }
    }
}

/*****************************************************************************
 �� �� ��: uart_data_process
 ��������  : �������ݴ�������
 �������  : ��
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ    :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void uart_data_process(void)
{
    INT8U uart_rx_len;

    uart_rx_len = uart_t.rx_buff_len;
    if((uart_t.delay_ms == 0) && (uart_rx_len > 0))
    {
        AT_command_deal();
        UART_RX_EN();
        
    }

    return;
}

/*****************************************************************************
 �� �� ��: AT_command_process
 ��������  : ATָ�������
 �������  : ��
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ    :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void AT_command_deal()
{
    INT8U i;

    if(mem_cmp(uart_buffer, "AT ", 3))
    {
        uart_tx_data("OK", 2);
        return;
    }

    if(mem_cmp(uart_buffer, "AT+", 3))
    {
        if(mem_cmp(&uart_buffer[3], "IPR=", 4))
        {
            for(i = 0; i < sizeof(atSetBaud)/4; i ++)
            {
                if(mem_cmp(&uart_buffer[7], atSetBaud[i], sizeof(atSetBaud[i])))
                {  
                    uart_tx_data("OK", 3);
                    uart_t.baud = uartBaud[i];

                    return;
                }
            }

            uart_tx_data("ERROR", 5);
            return;
        }

        if(mem_cmp(&uart_buffer[3], "CCLK", 4))
        {
            if(mem_cmp(&uart_buffer[7], "? ", 2))
            {
                uart_buffer[0]  = '+';
                mem_cpy(&uart_buffer[1], "CCLK", 4);
                uart_buffer[5]  = ':';
                uart_buffer[6]  = '"';    
                uart_buffer[17] = ',';
                uart_buffer[26] = ' ';
                uart_buffer[27] = sys_time_t.week_day + 0x30;
                uart_buffer[28] = ' ';
                uart_buffer[29] = '"';
                get_system_time(&uart_buffer[7],  &sys_time_t, 1);
                get_system_time(&uart_buffer[18], &sys_time_t, 0);
                uart_tx_data(uart_buffer, 30);

                return;
            }
            else if(uart_buffer[7] == '=' &&\
                    uart_buffer[8] == '"' &&\
                    ('0' <= uart_buffer[9]  && uart_buffer[9]  <= '9') &&\
                    ('0' <= uart_buffer[10] && uart_buffer[10] <= '9') &&\
                    ('0' <= uart_buffer[11] && uart_buffer[11] <= '9') &&\
                    ('0' <= uart_buffer[12] && uart_buffer[12] <= '9') &&\
                    uart_buffer[13] == '/' &&\
                    ('0' <= uart_buffer[14] && uart_buffer[14] <= '9') &&\
                    ('0' <= uart_buffer[15] && uart_buffer[15] <= '9') &&\
                    uart_buffer[16] == '/' &&\
                    ('0' <= uart_buffer[17] && uart_buffer[17] <= '9') &&\
                    ('0' <= uart_buffer[18] && uart_buffer[18] <= '9') &&\
                    uart_buffer[19] == ',' &&\
                    ('0' <= uart_buffer[20] && uart_buffer[20] <= '2') &&\
                    ('0' <= uart_buffer[21] && uart_buffer[21] <= '9') &&\
                    uart_buffer[22] == ':' &&\
                    ('0' <= uart_buffer[23] && uart_buffer[23] <= '6') &&\
                    ('0' <= uart_buffer[24] && uart_buffer[24] <= '9') &&\
                    uart_buffer[25] == ':' &&\
                    ('0' <= uart_buffer[26] && uart_buffer[26] <= '6') &&\
                    ('0' <= uart_buffer[27] && uart_buffer[27] <= '9') &&\
                    uart_buffer[28] == '"' &&\
                    uart_buffer[29] == ' ')
                    {
                        sys_time_t.year       = (uart_buffer[9]  - 0x30) * 1000 + (uart_buffer[10] - 0x30) * 100 +\
                                                        (uart_buffer[11] - 0x30) * 10   + (uart_buffer[12] - 0x30);
                        sys_time_t.month      = (uart_buffer[14] - 0x30) * 10 + uart_buffer[15] - 0x30;
                        sys_time_t.month_day  = (uart_buffer[17] - 0x30) * 10 + uart_buffer[18] - 0x30;
                        sys_time_t.hour       = (uart_buffer[20]  - 0x30) * 10 + uart_buffer[21] - 0x30;
                        sys_time_t.min        = (uart_buffer[23] - 0x30) * 10 + uart_buffer[24] - 0x30;
                        sys_time_t.sec        = (uart_buffer[26] - 0x30) * 10 + uart_buffer[27] - 0x30;

                        system_ui_t.delay_s = 0;
                        uart_tx_data("OK", 2);
                    }
                    else
                    {
                        uart_tx_data("ERROR", 5);
                        return;
                    }
        }
   }
}

